package com.xyz.moduledemo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.xyz.basesdk.base.BaseActivity;

public class MainActivity extends BaseActivity {

    private TextView title;
    private Fragment currentFragment;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    title.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    title.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    title.setText(R.string.title_notifications);
                    return true;
                default:
                    break;
            }
            return false;
        }
    };
    private Fragment eyeFragment;

    @Override
    protected boolean enableSliding() {
        return false;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        title = findViewById(R.id.title);
        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    @Override
    protected void init() {
        eyeFragment = (Fragment) ARouter.getInstance().build("/eye/eyeFragment").navigation();
        changeFg(eyeFragment, "eye");
    }

    @Override
    protected void onBackCancel() {

    }

    /**
     * 切换fragment
     *
     * @param fragment 要显示的fragment
     * @param tag      fragment对应tag
     */
    public void changeFg(Fragment fragment, String tag) {
        if (null == fragment) return;
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!fragment.isAdded()) {
            transaction.replace(R.id.content, fragment, tag)
                    .commitAllowingStateLoss();
            currentFragment = fragment;
        } else {
            transaction.remove(fragment).commitAllowingStateLoss();
        }
    }
}
